import { Context } from 'egg';

export default () => {
    return async function token(ctx: Context, next) {
        const urlArray: string[] = ['/api/user/searchUserById', '/api/user/editUserStatus',
            '/api/setting/updateSetting', '/api/paint/searchPaintList',
            '/api/paint/createPaint', '/api/paint/startAndStopPaint', '/api/paint/removePaint',
            '/api/paint/searchPaintById', '/api/paint/updatePaint', '/api/paint/searchPaintValueById',
            '/api/paint/deletePaintValue', '/api/paint/removePaintValue',
            '/api/advert/createAdvert', '/api/advert/updateAdvert',
            '/api/advert/startAndStopAdert', '/api/advert/removeAdvert',
            '/api/school/createSchool', '/api/school/startAndStopSchool', '/api/school/searchSchoolById',
            '/api/school/createCollege', '/api/school/startAndStopCollege', '/api/school/updateSchool',
            '/api/topic/createTopic', '/api/topic/updateTopic', '/api/topic/startAndStopTopic',
            '/api/activity/searchActivityList', '/api/activity/startAndStopActivity',
            '/api/activity/createActivity', '/api/feedback/searchFeedbackList',
            '/api/auditing/searchUserList', '/api/auditing/auditingUser', '/api/assuageGrief/searchAssuageGriefList',
            '/api/assuageGrief/searchTopicByTitle', '/api/assuageGrief/showAndShieldAssuageGrief',
            '/api/index/getActivityNum', '/api/index/getRegisterNum', '/api/index/getClickNum',
            '/api/index/getBrowseTime', '/api/accusation/searchAccusationAll', '/api/accusation/editAccusationStatus',
            '/api/accusation/getSystemExcuseTime', '/api/school/searchCollegeBySchoolId', '/api/admin/createAdmin',
            '/api/admin/deleteAdmin', '/api/admin/searchAdminByParam', '/api/admin/updateAdmin',
            '/api/admin/startAndStopAdmin', '/api/school/updateCollege', '/api/wx/wxPlatform/sendNotifications',
            '/api/wx/wxPlatform/getTemplateList', '/api/activity/endingAdminActivity',
            '/api/wx/wxPlatform/deleteTemplate', '/api/wx/wxPlatform/searchTemplate',
            '/api/wx/wxPlatform/editTemplate'];
        const token = ctx.headers.token;
        let url = ctx.originalUrl;
        let isNeedToken: boolean = true;
        for (const value of urlArray) {
            if (value === url) {
                const admin = await ctx.service.adminService.searchAdminByToken(token);
                if (admin) {
                    await next();
                    isNeedToken = false;
                    break;
                }
            }
        }
        if (!isNeedToken) {
            return;
        } else {
            await next();
        }
    };
};
